﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Template.Infrastructure.Extensions;
using Template.Infrastructure.Filter;

namespace Template.Infrastructure.Reflection
{
    /// <summary>
    /// Attribute类型查找器基类
    /// </summary>
    /// <typeparam name="TAttributeType"></typeparam>
    public abstract class AttributeTypeFinderBase<TAttributeType> : FinderBase<Type>, ITypeFinder where TAttributeType: Attribute
    {


        private readonly IAllAssemblyFinder _allAssemblyFinder;

        protected AttributeTypeFinderBase(IAllAssemblyFinder allAssemblyFinder)
        {
            _allAssemblyFinder = allAssemblyFinder;
        }

        /// <summary>
        /// 重写以实现所有项的查找
        /// </summary>
        /// <returns></returns>
        protected override Type[] FindAllItems()
        {
            var assemblys = _allAssemblyFinder.FindAll(true);
            return assemblys.SelectMany(assembly => assembly.GetTypes()).Where(t => t.IsClass && !t.IsAbstract&&t.HasAttribute<TAttributeType>(true)).Distinct().ToArray();
        }
    }
}
